@echo off
cls
echo =====================================
echo   MyWallet - Build APK
echo =====================================
echo.
echo This will:
echo   1. Kill Java processes
echo   2. Clean cache
echo   3. Get dependencies
echo   4. Build APK (3-5 minutes)
echo.
echo Press any key to start...
pause >nul

cls
echo.
echo Building APK...
echo.

echo [Step 1/5] Killing Java processes...
taskkill /F /IM java.exe >nul 2>&1
taskkill /F /IM javaw.exe >nul 2>&1
timeout /t 2 /nobreak >nul
echo OK
echo.

echo [Step 2/5] Cleaning cache...
call flutter clean
if errorlevel 1 (
    echo ERROR: Clean failed!
    goto :error
)
echo OK
echo.

echo [Step 3/5] Getting dependencies...
call flutter pub get
if errorlevel 1 (
    echo ERROR: pub get failed!
    goto :error
)
echo OK
echo.

echo [Step 4/5] Building APK...
echo.
echo This will take 3-5 minutes...
echo.
echo Will generate:
echo   - app-armeabi-v7a-release.apk  (21MB - 32bit)
echo   - app-arm64-v8a-release.apk    (25MB - 64bit, recommended)
echo   - app-x86_64-release.apk       (27MB - emulator)
echo.
echo Building...
echo.

call flutter build apk --release --split-per-abi
if errorlevel 1 (
    echo.
    echo ERROR: Build failed!
    echo.
    echo Please check:
    echo   1. Android SDK installed
    echo   2. Network connection
    echo   3. Run: flutter doctor
    echo.
    goto :error
)

echo.
echo [Step 5/5] Opening folder...
if exist "build\app\outputs\flutter-apk" (
    start explorer build\app\outputs\flutter-apk
    echo OK
) else (
    echo WARNING: APK folder not found
)
echo.

:success
echo.
echo =====================================
echo Build Complete!
echo =====================================
echo.
echo APK Location: build\app\outputs\flutter-apk\
echo.
echo Recommended: app-arm64-v8a-release.apk
echo.
echo Press any key to exit...
pause >nul
exit /b 0

:error
echo.
echo =====================================
echo Build Failed
echo =====================================
echo.
pause >nul
exit /b 1
